package com.wind.B_WorkQueues;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者1
 *
 * @author Windy
 * @date 2021年3月2日 22:03:27
 */
public class Consumer1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("/wind");
        factory.setUsername("wind");
        factory.setPassword("wind");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("work_queues", true, false, false, null);  // 不需要返回值
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            // Ctrl + O：重写方法
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("body: " + new String(body));
            }
        };
        channel.basicConsume("work_queues", true, consumer); // 牢记3个参数：队列名称、自动应答、回调对象
    }
}
